
import graph

class Node:
    def __init__(self):
        pass


class OperatorNode(Node):
    def __init__(self,inputs,outputs):
        Node.__init__(self)
        self.inputs = {}
        for i in inputs:
            self.inputs[i] = None
        self.outputs = {}
        for i in outputs:
            self.outputs[i] = None
        self.makeGraph()

    def makeGraph(self):
        self.graph = graph.digraph()
        self.graph.add_node(self)
        for k in self.inputs.keys():
            self.graph.add_node(k)
            self.graph.add_edge(k,self)
        for k in self.outputs.keys():
            self.graph.add_node(k)
            self.graph.add_edge(self,k)
        print self.graph.write()
            
    def canProcess(self):
        vs = [v == None for v in self.inputs.values()]
        print vs
        return not any(vs)
        
    def setInput(self,name,value):
        self.inputs[name]=value
    def setOutputs(self):
        self.process()

class Add(OperatorNode):
    def __init__(self):
        OperatorNode.__init__(self,["A","B"],["Sum"])
    def process(self):
        self.outputs["Sum"] = self.inputs["A"]+self.inputs["B"]
    
class Sink(Node):
    def __init__(self):
        Node.__init__(self)
        self.input=None
    def setOutput(self,value):
        self.input = value
    def canProcess(self):
        return self.input != None
    def process(self):
        print self.input

    
        

def mainSink():
    s = Sink()
    print s.canProcess()
    s.setOutput(123)
    print s.canProcess()
    s.process()
    
def mainOp():
    s = Sink()
    add1 = Add()
    add1.setInput("A",4)
    add1.setInput("B",2)
    add1.setOutputs()
    add2 = Add()
    add2.setInput("A",3)
    add2.setInput("B",add1.outputs['Sum'])
    add2.setOutputs()
    s.setOutput(add2.outputs['Sum'])
    s.process()
    


if __name__=="__main__":
    mainOp()
